package com.sense.kukoo.common.wiget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.sense.kukoo.common.R
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class RecentlyListeningItemView: ConstraintLayout {

    private val TAG = RecentlyListeningItemView::class.simpleName
    private lateinit var iv_icon_listening_1:ImageView
    private lateinit var iv_icon_listening_2:ImageView
    private lateinit var iv_icon_listening_3:ImageView
    private lateinit var iv_icon_listening_1_fg:ImageView
    private lateinit var iv_icon_listening_2_fg:ImageView
    private lateinit var iv_icon_listening_3_fg:ImageView
    private lateinit var tv_number_listening:TextView

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        initView()
    }

    private fun initView(){
        LayoutInflater.from(context).inflate(R.layout.common_item_recently_listening, this, true)
        iv_icon_listening_1 = findViewById<ImageView>(R.id.iv_icon_listening_1)
        iv_icon_listening_2 = findViewById<ImageView>(R.id.iv_icon_listening_2)
        iv_icon_listening_3 = findViewById<ImageView>(R.id.iv_icon_listening_3)
        iv_icon_listening_1_fg = findViewById<ImageView>(R.id.iv_icon_listening_1_fg)
        iv_icon_listening_2_fg = findViewById<ImageView>(R.id.iv_icon_listening_2_fg)
        iv_icon_listening_3_fg = findViewById<ImageView>(R.id.iv_icon_listening_3_fg)
        tv_number_listening = findViewById<TextView>(R.id.tv_number_listening)
    }

    fun setIconUrls(urls: ArrayList<String>){
        val ivs = arrayOf(iv_icon_listening_1, iv_icon_listening_2, iv_icon_listening_3)
        val ivFgs = arrayOf(iv_icon_listening_1_fg, iv_icon_listening_2_fg, iv_icon_listening_3_fg)
        for (index in 0..2){
            if(urls.size > index){
                val url = urls[index]
                val iv = ivs[index]
                Monet.get(context)
                    .load<GlideConfig>(url)
                    .placeholder(com.sense.kukoo.common_design.R.drawable.design_icon_avatar_default)
                    .circleCrop()
                    .into(iv)
            } else {
                ivs[index].visibility = View.GONE
                ivFgs[index].visibility = View.GONE
            }
        }
    }

    fun setNumber(num: String){
        tv_number_listening.text = num
    }
}